package com.api.interceptor;

import com.api.annotation.TestAnnotation;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

/**
 * Created by rio on 2017/1/17.
 */
public class TestInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(handler instanceof HandlerMethod)
        {
            Method method=((HandlerMethod) handler).getMethod();
            TestAnnotation annotation=method.getAnnotation(TestAnnotation.class);
            if(annotation!=null)
            {
                String token=request.getParameter("token");
                if(!StringUtils.isEmpty(token))
                {
                    request.getRequestDispatcher("" + "9001").forward(request, response);
                    return false;
                }
            }
        }
        return  true;

    }
}
